title: “Creative Assignment 4” author: “Maggie Weese” date: “10/1/2020” output: html_document: theme: spacelab toc: true toc_float: true code_folding: hide — # Creating Isochrones in R Studio Thank you to Carole and Alex for the base code for this exercise!

Loading Libraries and Data

library(osmdata)
library(opentripplanner)
library(tidyverse)
library(sf)
library(ggthemes)
library(ggspatial)

BPP_libraries is data on public pools in Baltimore sourced from the Baltimore City Data Directory. Bmore_schools is data on all public schools in Baltimore also sourced from the Baltimore City Data Directory.

BPP_libraries <- st_read(
  "https://data.baltimorecity.gov/api/geospatial/2fex-mveq?method=export&format=KML")
Bmore_schools <- st_read(
  "https://data.baltimorecity.gov/api/geospatial/y4x7-8za4?method=export&format=KML")

Using Open Trip Planner

This code searches for streets in Baltimore, MD in Open Trip Planner and save it onto my desktop.

opq(bbox = 'Baltimore MD USA') %>%
  add_osm_feature(key = 'highway') %>%
  osmdata_xml(file = 'OTP/graphs/default/Baltimore_Streets.osm')

Changing Projections

In this section, I shift the projection to be the Baltimore City projection found on the site Spatial Reference. I also create the variables used in the map below.

Bmore_state_plane <- "+proj=omerc +lat_0=39.304635 +lonc=-76.614429 +alpha=0 +k=1 +x_0=106.4881889763779 +y_0=788.8544577089153 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs"
bmore_street_features <- opq(bbox = 'Baltimore MD USA') %>%
  add_osm_feature(key = 'highway') %>%
  osmdata_sf()
bmore_streets <- bmore_street_features$osm_lines %>%
  st_transform(crs = Bmore_state_plane)

Initial Map of OTP Data

Below is a map of the street networks in Baltimore City, MD.

ggplot(bmore_streets) +
  geom_sf() +
  theme_map()

Mapping Isochrones from OTP Data

Connection to OTP

path_otp <- otp_dl_jar("OTP")
path_data <- file.path(getwd(), "OTP")
path_otp <- paste(path_data, "otp.jar",sep = "/")
otp_build_graph(otp = path_otp, dir = path_data, memory = 1024) 
otp_setup(otp = path_otp, dir = path_data, memory =1024)
# Connect to opentripplanner
otpcon <- otp_connect()
## Router http://localhost:8080/otp/routers/default exists

Creating Isochrone Data for 5 minute Walking and Driving Radius from Baltimore City Public Pools.

iso_5min_walk <- 
  otp_isochrone(otpcon = otpcon, fromPlace = BPP_libraries, 
                mode = "WALK", cutoffSec = 300) %>%
  st_transform(crs = Bmore_state_plane) %>%
  mutate(mode = "walk")
iso_5min_drive <- 
  otp_isochrone(otpcon = otpcon, fromPlace = BPP_libraries, 
                mode = "CAR", cutoffSec = 300) %>%
  st_transform(crs = Bmore_state_plane) %>%
  mutate(mode = "drive")
iso_all_modes <- rbind(iso_5min_drive, iso_5min_walk)

Mapping Isochrones for Walking and Driving

right_side <- st_bbox(iso_all_modes)$xmax
left_side  <- st_bbox(iso_all_modes)$xmin
top_side <- st_bbox(iso_all_modes)$ymax
bottom_side <- st_bbox(iso_all_modes)$ymin
ggplot(iso_all_modes) +
  annotation_map_tile(zoomin = 0, type = "cartolight", progress = "none") +
  geom_sf(aes(fill = mode), alpha = 0.5) +
  geom_sf(data = BPP_libraries) +
  coord_sf(xlim = c(left_side, right_side), 
           ylim = c(bottom_side, top_side), expand = FALSE) +
  scale_fill_viridis_d(name = "Area that is reachable within 5 minutes",
                       labels = c("By car", "By foot")) +
annotation_north_arrow(location = "tr", which_north = "true", 
        pad_x = unit(2, "in"), pad_y = unit(7, "in"),
        style = north_arrow_fancy_orienteering)  +
        annotation_scale(location="br") + 
        theme_map()+
        labs(caption = "Basemap Copyright OpenStreetMap contributors")

Graph of Comparing Area of Driveshed to Walkshed

iso_areas <- iso_all_modes %>%
  mutate(area = st_area(iso_all_modes)) %>%
  st_set_geometry(NULL) %>%
  pivot_wider(names_from = mode, values_from = area) 
ggplot(iso_areas, 
       aes(x = as.numeric(walk), y = as.numeric(drive))) +
  geom_point() +
  scale_x_continuous(name = 
            "Area within a five-minute walking distance\nof a public pool\n(square km)",
            breaks = breaks <- seq(0, 2000000, by = 200000),
            labels = breaks / 1000000) +
  scale_y_continuous(name = 
            "Area within a five-minute driving distance\nof a public pool\n(square km)",           
            breaks = breaks <- seq(0, 15000000, by = 1500000),
            labels = breaks / 1000000) + 
  ggtitle("Area Within 5 Min Driving Distance of Public Pool vs 5 Min Walking Distance") + 
  theme_bw()

iso_5min_walk <- 
  otp_isochrone(otpcon = otpcon, fromPlace = BPP_libraries, 
                mode = "WALK", cutoffSec = 300) %>%
  st_transform(crs = Bmore_state_plane) %>%
  mutate(mode = "walk")
iso_5min_bike <- 
  otp_isochrone(otpcon = otpcon, fromPlace = BPP_libraries, 
                mode = "BICYCLE", cutoffSec = 300) %>%
  st_transform(crs = Bmore_state_plane) %>%
  mutate(mode = "bike")
iso_5min_drive <- 
  otp_isochrone(otpcon = otpcon, fromPlace = BPP_libraries, 
                mode = "CAR", cutoffSec = 300) %>%
  st_transform(crs = Bmore_state_plane) %>%
  mutate(mode = "drive")
iso_all_modes2 <- rbind(iso_5min_walk, iso_5min_drive, iso_5min_bike)

Mapping Isochrones for Walking, Driving, and Biking

right_side <- st_bbox(iso_all_modes2)$xmax
left_side  <- st_bbox(iso_all_modes2)$xmin
top_side <- st_bbox(iso_all_modes2)$ymax
bottom_side <- st_bbox(iso_all_modes2)$ymin
ggplot(iso_all_modes2) +
  annotation_map_tile(zoomin = 0, type = "cartolight", progress = "none") +
  geom_sf(aes(fill = mode), alpha = 0.5) +
  geom_sf(data = BPP_libraries) +
  coord_sf(xlim = c(left_side, right_side), 
           ylim = c(bottom_side, top_side), expand = FALSE) +
  scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"),
                      name = "Area that is reachable within 5 minutes",
                      labels = c("By bike", "By car", "By foot")) +
  labs(caption = "Basemap Copyright OpenStreetMap contributors") +
   annotation_scale(location="br") + 
  annotation_north_arrow(location = "tr", which_north = "true", 
        pad_x = unit(2, "in"), pad_y = unit(7, "in"),
        style = north_arrow_fancy_orienteering)  +
  theme_map()

Creating Map of Public Schools within 5 min Drive of a Public Pool

Selecting Schools within 5 min Driveshed of Public Pools

Bmore_schools <- Bmore_schools %>% 
  st_transform(Bmore_state_plane)
drive_schools <- Bmore_schools[iso_5min_drive,]
ggplot(iso_5min_drive) +
  geom_sf() +
  geom_sf(data = drive_schools, 
          color = "blue", 
          size = 1) +
  theme_map()

Calculating Number and Percentage of Schools within Driveshed

Bmore_schools <- Bmore_schools %>%
  st_join(drive_schools) %>%
  mutate(by_pool = !is.na(Name.y))
n_drive_schools <- sum(Bmore_schools$by_pool)
n_drive_schools
## [1] 23
n_Bmore_schools <- length(Bmore_schools$by_pool)
pct_drive_schools <- n_drive_schools / n_Bmore_schools
pct_drive_schools
## [1] 0.1314286

Mapping Public Schools within 5 min Drive of a Public Pool

right_side <- st_bbox(iso_all_modes)$xmax
left_side  <- st_bbox(iso_all_modes)$xmin
top_side <- st_bbox(iso_all_modes)$ymax
bottom_side <- st_bbox(iso_all_modes)$ymin
ggplot(iso_5min_drive) +
  annotation_map_tile(zoomin = 0, type = "cartolight", progress = "none") +
  geom_sf(fill = "seashell1", alpha = 0.5) +
  geom_sf(data = Bmore_schools, size = 1.5,
          aes(color = by_pool)) +
  scale_color_manual(values = c("deepskyblue2", "darkorchid"),
          name = "Baltimore City \nPublic Schools\nNear Public Pools",
          labels = c("No School within 5 min drive",
                     "School within 5 min drive"))+
  coord_sf(xlim = c(left_side, right_side), 
           ylim = c(bottom_side, top_side), expand = FALSE) +
  scale_fill_discrete(guide=FALSE) +
  theme_map() +
  annotate(geom = "text", x = left_side, 
           y = top_side,  fontface = "italic",
           label = paste("  Of the ", 
                         prettyNum(n_Bmore_schools, big.mark = ","), 
                         " public schools\n  in Baltimore, ", 
                         prettyNum(n_drive_schools, big.mark = ","),
                         " (", 
                         prettyNum(100*pct_drive_schools, digits = 0),
                         "%) are\n within a 5 minute drive from a \n    public pool.",
                         sep = ""),
           hjust = -0.1, vjust = 1.5, size = 4) +
  labs(caption = "Basemap Copyright OpenStreetMap contributors")+
  annotation_north_arrow(location = "tr", which_north = "true", 
        pad_x = unit(2, "in"), pad_y = unit(7, "in"),
        style = north_arrow_fancy_orienteering)  +
        annotation_scale(location="br") +

  theme_map()

otp_stop()
## [1] "SUCCESS: The process \"java.exe\" with PID 1820 has been terminated."